// init
package main

import (
	"flag"
	"fmt"

	"git.oschina.net/unphp/go-utils/base"
	"git.oschina.net/unphp/go-utils/conf"
	"git.oschina.net/unphp/go-utils/log"
	"git.oschina.net/unphp/go-utils/socket"
	"git.oschina.net/unphp/gowebstatic/controller"
	config "github.com/gokyle/goconfig"
)

// 批量定义全局变量
var (
	ConfData             *conf.Conf //配置
	Log                  *log.Log   //日志
	RootPath             string     //根目录
	ServerDeferFuncSlice []func()   //服务销毁前的回调函数

)

func init() {

	//初始化ServerDeferFuncSlice
	ServerDeferFuncSlice = make([]func(), 0)

	//服务的根目录（绝对路径）
	RootPath = base.RootPath()
	//fmt.Println(RootPath, "ssssssssssssssssssssss")
	//默认的配置文件路径
	defaultConfFile := RootPath + "/conf/conf.ini"
	//fmt.Println(defaultConfFile)
	configfile := flag.String("conf", defaultConfFile, "The conf of path")
	flag.Parse()

	fmt.Println(*configfile)
	//设置配置文件路径
	if initconf, err := config.ParseFile(*configfile); err != nil {
		panic("conf.ini can't open")
	} else {
		ConfData = &conf.Conf{
			Initconf: initconf,
		}
	}

	//系统日志
	Log = log.New()

	//runSocket
	runSocket()

}

func runSocket() {
	//启动Socket服务
	host := ConfData.Get("core", "sockethost").String()
	protocol := ConfData.Get("core", "socketprotocol").String()
	socketport := ConfData.Get("core", "socketport").String()

	appRouter := new(AppRouter)
	socket.AddModulesRoute("app", appRouter) //添加路由模块
	appRouter.AddRouterController("img", func() ControllerInterface { c := new(controller.ImgController); c.AppController = c; return c })
	go socket.NewSockey(protocol, host, socketport).Run() //运行socket服务
}

// 系统日志句柄函数
func sysLogHander(s string) {
	Log.Println(s)

}
